﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Raise3000.Code;

namespace Raise3000
{
    public enum OperatingMode
    {
        eUNKNOWN,
        eCREATENEW,
        eEDITEXISTING,
        eDELETE,
    };

    public partial class PlayerTemplateDialog : Form
    {
        // properties
        private OperatingMode OperatingMode_ = OperatingMode.eUNKNOWN;
        private PlayerTemplate PlayerTemplate_ = null;


        // property implementation
        public OperatingMode Mode
        {
            get
            {
                return OperatingMode_;
            }
            set
            {
                OperatingMode_ = value;
            }
        }

        public PlayerTemplate PTemplate
        {
            get
            {
                return PlayerTemplate_;
            }
            set
            {
                PlayerTemplate_ = value;
            }
        }
        
        public PlayerTemplateDialog()
        {
            InitializeComponent();
        }

        // methods
        public PlayerTemplate GetPlayerTemplate()
        {
            return PlayerTemplate_;
        }

        private void NewButton_Click(object sender, EventArgs e)
        {
            if (OperatingMode_ == OperatingMode.eCREATENEW)
            {
                PlayerTemplate_.Name = TemplateNameTextBox.Text;
                SaveValues(GeneralGroupBox);
                SaveValues(PF_GroupBox);
                SaveValues(FlopGroupBox);
                SaveValues(TurnGroupBox);
                SaveValues(RiverGroupBox);
            }

            DialogResult = DialogResult.OK;
            Close();
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            const string message = "Are you sure that you would like to close the form?";
            const string caption = "Form Closing";

            DialogResult result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
             
            if (result == DialogResult.Yes)
            {
                DialogResult = DialogResult.Cancel;
                Close();
            }
        }

        private void PF_AFqTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void PF_AFTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
        // 
        private void SaveValues(GroupBox gb)
        {
            foreach (Control c in gb.Controls)
            {
                if (c is TextBox)
                {
                    TextBox tb = (TextBox)c;
                    string attributeName = tb.Name;
                    StatisticalAttribute sa = PlayerTemplate_.FindAttributeByName(attributeName);
                    if (sa == null)
                    {
                        if (tb.Text != "")
                        {
                            StatisticalAttribute saNew = new StatisticalAttribute(tb.Name, Double.Parse(tb.Text));
                            PlayerTemplate_.AddAttribute(saNew);
                        }
                    }
                    else
                    {
                        if (tb.Text != "")
                        {
                            // overwriting the existing
                            sa.Value = Double.Parse(tb.Text);
                        }
                    }
                }
            }

        }

        private void LoadValues(GroupBox gb)
        {
            foreach (Control c in gb.Controls)
            {
                if (c is TextBox)
                {
                    StatisticalAttribute sa = PlayerTemplate_.FindAttributeByName(c.Name);
                    if (sa != null)
                    {
                        double value = sa.Value;
                        c.Text = Convert.ToString(value);
                    }
                }
            }
        }

        private void PlayerTemplateDialog_Load(object sender, EventArgs e)
        {
            // load names, description, attributes ....
            if (OperatingMode_ == OperatingMode.eEDITEXISTING)
            {
                TemplateNameTextBox.Text = PlayerTemplate_.Name;
                DescriptionTextBox.Text = PlayerTemplate_.Description;

                // load attributes
                LoadValues(GeneralGroupBox);
                LoadValues(PF_GroupBox);
                LoadValues(FlopGroupBox);
                LoadValues(TurnGroupBox);
                LoadValues(RiverGroupBox);
            }
        }
    }
}
